#include <scene/GeometryNodes.h>

Matrix::Matrix() {
  D3DXMatrixIdentity(&matrix);
}

Matrix::Matrix(const Matrix * rhs) {
  matrix = rhs->matrix;
}

long Matrix::preDraw(Device * device) {
  return device->pushWorldMatrix(&matrix);
}

long Matrix::postDraw(Device * device) {
  return device->popWorldMatrix();
}

SceneNode * Matrix::copy() const {
  return new Matrix(this);
}

SceneNode * Matrix::select(const D3DXVECTOR4 & v){
  D3DXVECTOR4 v_trans;
  D3DXMATRIX m_inverse;

  D3DXMatrixInverse(&m_inverse, NULL, &matrix);

  D3DXVec4Transform(&v_trans, &v, &m_inverse);
  return SceneNode::select(v_trans);
}

Perspective::Perspective() {
  width = 1.0;
  height = 1.0;
}

Perspective::Perspective(const Perspective * rhs) {
  width = rhs->width;
  height = rhs->height;
}

long Perspective::preDraw(Device * device) {
  device->resize(width, height);
  return 0;
}

SceneNode * Perspective::copy() const {
  return new Perspective(this);
}

SceneNode * Perspective::select(const D3DXVECTOR4 & v){
  D3DXVECTOR4 v_trans;
  D3DXMATRIX m;
  D3DXMATRIX m_inverse;
  
  D3DXMatrixOrthoRH(&m, width, height, -100.0f, 100.0f);

  D3DXMatrixInverse(&m_inverse, NULL, &m);

  D3DXVec4Transform(&v_trans, &v, &m_inverse);
  return SceneNode::select(v_trans);
}
